<% cache "tag-sidebar-#{@tag.name}-#{@tag&.updated_at}-#{user_signed_in?}-#{@page}", expires_in: 72.hours do %>
  <div id="sidebar-wrapper-left" class="sidebar-wrapper sidebar-wrapper-left">
    <div class="sidebar-bg" id="sidebar-bg-left"></div>
    <aside class="side-bar">
      <% if @tag %>
        <%= application_policy_content_tag("div", record: Article, query: :create?, class: "widget") do %>
          <a class="crayons-btn crayons-btn--s" href="/new/<%= @tag.name %>">
            <%= t("views.tags.sidebar.new") %>
          </a>
        <% end %>
        <% if @tag.rules_html.present? %>
          <div class="widget">
            <header>
              <h4><%= t("views.tags.sidebar.guidelines") %></h4>
            </header>
            <div class="widget-body">
              <%= sanitize @tag.rules_html %>
            </div>
          </div>
        <% end %>
        <% if @tag.wiki_body_html.present? %>
          <div class="widget">
            <header>
              <h4><%= t("views.tags.sidebar.about", tag: @tag.name) %></h4>
            </header>
            <div class="widget-body">
              <%= sanitize @tag.wiki_body_html %>
            </div>
          </div>
        <% end %>
      <% end %>
      <% if user_signed_in? && @moderators.present? %>
        <div class="widget">
          <header>
            <h4><%= t("views.tags.sidebar.mods") %></h4>
          </header>
          <div class="widget-body">
            <% @moderators.each do |user| %>
              <div class="widget-user-pic">
                <a href="/<%= user.username %>" title="<%= user.username %>">
                  <img src="<%= user.profile_image_url_for(length: 90) %>" alt="<%= user.username %> profile image">
                </a>
              </div>
            <% end %>
          </div>
        </div>
      <% end %>
      <div class="sidebar-data">
        <% if user_signed_in? %>
          <div class="crayons-btn-group mt-3">
            <a style="display:none;"
                id="tag-edit-button"
                data-tag="<%= @tag.name %>"
                class="crayons-btn crayons-btn--outlined mod-action-button fs-s"
                href="<%= tag_url(@tag, @page) %>/edit"
                style="color:<%= @tag.text_color_hex %>;background-color:<%= @tag.bg_color_hex %>">
              <%= t("views.tags.sidebar.edit") %>
            </a>
            <a style="display:none;"
               id="tag-admin-button"
               data-tag="<%= @tag.name %>"
               class="crayons-btn crayons-btn--outlined mod-action-button fs-s"
               href="<%= edit_admin_tag_path(@tag.id) %>"
               style="color:<%= @tag.text_color_hex %>;background-color:<%= @tag.bg_color_hex %>"
               data-no-instant>
              <%= t("views.tags.sidebar.admin") %>
            </a>
            <a style="display:none;"
               id="tag-mod-button"
               data-tag="<%= @tag.name %>"
               class="crayons-btn crayons-btn--outlined mod-action-button fs-s"
               href="/mod/<%= @tag.name %>"
               style="color:<%= @tag.text_color_hex %>;background-color:<%= @tag.bg_color_hex %>">
              <%= t("views.tags.sidebar.moderate") %>
            </a>
          </div>
        <% else %>
          <% number_of_pages = (@num_published_articles.to_f / @number_of_articles).ceil %>
          <% range = number_of_pages < 10 || @page < 4 ? 1..[number_of_pages, 9].min : [@page - 3, 1].max..[@page + 5, number_of_pages].min %>
          <% if number_of_pages > 1 %>
            <hr />
            <div class="olderposts-header"><%= t("views.tags.sidebar.older", tag: @tag.name) %></div>
            <div class="olderposts-links">
              <% range.each do |n| %>
                <% if @page == n %>
                    <span class="olderposts-pagenumber"><%= n %></span>
                <% else %>
                    <a href="<%= tag_url(@tag, n) %>" class="olderposts-pagenumber"><%= n %></a>
                <% end %>
              <% end %>
              <% if @page == 1 && number_of_pages > 100 %>
                <div>
                  …<a href="<%= tag_url(@tag, 75) %>" class="olderposts-pagenumber">75</a>
                  …<a href="<%= tag_url(@tag, number_of_pages) %>" class="olderposts-pagenumber"><%= number_of_pages %></a>
                </div>
              <% end %>
            </div>
          <% end %>
        <% end %>
      </div>
    </aside>
  </div>
<% end %>
